/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using Xceed.Silverlight.Data.Stats;

namespace Xceed.Silverlight.Data
{
  public abstract class SyncIndexDataSourceProvider : IndexDataSourceProvider
  {
    #region Static Fields

    private static readonly Guid ExecuteCountQueryCallId = DataUtils.CreateCallId();
    private static readonly Guid ExecuteDataQueryCallId = DataUtils.CreateCallId();
    private static readonly Guid ExecuteFindQueryCallId = DataUtils.CreateCallId();
    private static readonly Guid ExecuteGroupCountQueryCallId = DataUtils.CreateCallId();
    private static readonly Guid ExecuteStatQueryCallId = DataUtils.CreateCallId();

    #endregion

    #region Constructor

    protected SyncIndexDataSourceProvider()
      : base()
    {
    }

    #endregion

    #region IsAsynchronous Internal Property

    internal sealed override bool IsAsynchronous
    {
      get
      {
        return false;
      }
    }

    #endregion

    #region IsCachingAllowed Internal Property

    internal override bool IsCachingAllowed
    {
      get
      {
        return false;
      }
    }

    #endregion

    #region IsBufferingAllowed Internal Property

    internal override bool IsBufferingAllowed
    {
      get
      {
        return false;
      }
    }

    #endregion

    protected abstract CountQueryResult ExecuteCountQuery( CountQueryParameters queryParameters );

    protected abstract DataQueryResult ExecuteDataQuery( DataQueryParameters queryParameters );

    protected abstract FindQueryResult ExecuteFindQuery( FindQueryParameters queryParameters );

    protected abstract GroupCountQueryResult ExecuteGroupCountQuery( GroupCountQueryParameters queryParameters );

    protected abstract StatQueryResult ExecuteStatQuery( StatQueryParameters queryParameters );

    internal sealed override IAsyncResult BeginExecuteCountQueryIndexed(
      CountQueryParameters queryParameters,
      AsyncCallback callback,
      object state )
    {
      CountQueryResult queryResult = this.ExecuteCountQuery( queryParameters );

      AsyncResult asyncResult = new AsyncResult(
                                  SyncIndexDataSourceProvider.ExecuteCountQueryCallId,
                                  callback,
                                  state );
      asyncResult.StartSync( queryResult );

      return asyncResult;
    }

    internal sealed override CountQueryResult EndExecuteCountQueryIndexed( IAsyncResult asyncResult )
    {
      asyncResult.ValidateCallEnd( SyncIndexDataSourceProvider.ExecuteCountQueryCallId );

      return asyncResult.GetCallResult<CountQueryResult>();
    }

    internal sealed override IAsyncResult BeginExecuteDataQueryIndexed(
      DataQueryParameters queryParameters,
      AsyncCallback callback,
      object state )
    {
      DataQueryResult queryResult = this.ExecuteDataQuery( queryParameters );

      AsyncResult asyncResult = new AsyncResult(
                                  SyncIndexDataSourceProvider.ExecuteDataQueryCallId,
                                  callback,
                                  state );
      asyncResult.StartSync( queryResult );

      return asyncResult;
    }

    internal sealed override DataQueryResult EndExecuteDataQueryIndexed( IAsyncResult asyncResult )
    {
      asyncResult.ValidateCallEnd( SyncIndexDataSourceProvider.ExecuteDataQueryCallId );

      return asyncResult.GetCallResult<DataQueryResult>();
    }

    internal sealed override IAsyncResult BeginExecuteFindQueryIndexed(
      FindQueryParameters queryParameters,
      AsyncCallback callback,
      object state )
    {
      FindQueryResult queryResult = this.ExecuteFindQuery( queryParameters );

      AsyncResult asyncResult = new AsyncResult(
                                  SyncIndexDataSourceProvider.ExecuteFindQueryCallId,
                                  callback,
                                  state );
      asyncResult.StartSync( queryResult );

      return asyncResult;
    }

    internal sealed override FindQueryResult EndExecuteFindQueryIndexed( IAsyncResult asyncResult )
    {
      asyncResult.ValidateCallEnd( SyncIndexDataSourceProvider.ExecuteFindQueryCallId );

      return asyncResult.GetCallResult<FindQueryResult>();
    }

    internal sealed override IAsyncResult BeginExecuteGroupCountQueryIndexed(
      GroupCountQueryParameters queryParameters,
      AsyncCallback callback,
      object state )
    {
      GroupCountQueryResult queryResult = this.ExecuteGroupCountQuery( queryParameters );

      AsyncResult asyncResult = new AsyncResult(
                                  SyncIndexDataSourceProvider.ExecuteGroupCountQueryCallId,
                                  callback,
                                  state );
      asyncResult.StartSync( queryResult );

      return asyncResult;
    }

    internal sealed override GroupCountQueryResult EndExecuteGroupCountQueryIndexed( IAsyncResult asyncResult )
    {
      asyncResult.ValidateCallEnd( SyncIndexDataSourceProvider.ExecuteGroupCountQueryCallId );

      return asyncResult.GetCallResult<GroupCountQueryResult>();
    }

    internal sealed override IAsyncResult BeginExecuteStatQueryIndexed( 
      StatQueryParameters queryParameters, 
      AsyncCallback callback, 
      object state )
    {
      StatQueryResult queryResult;

      try
      {
        queryResult = this.ExecuteStatQuery( queryParameters );
      }
      catch( Exception e )
      {
        queryResult = new StatQueryResult( new StatResult( e ) );
      }

      AsyncResult asyncResult = new AsyncResult(
                                  SyncIndexDataSourceProvider.ExecuteStatQueryCallId,
                                  callback,
                                  state );
      asyncResult.StartSync( queryResult );

      return asyncResult;
    }

    internal sealed override StatQueryResult EndExecuteStatQueryIndexed( IAsyncResult asyncResult )
    {
      asyncResult.ValidateCallEnd( SyncIndexDataSourceProvider.ExecuteStatQueryCallId );

      return asyncResult.GetCallResult<StatQueryResult>();
    }
  }
}
